home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / MPW C ƒ / MPW UnShar ƒ / MPW Unshar.c
Encoding:
C/C++ Source or Header  |  1990-04-02  |  4.8 KB  |  129 lines  |  [TEXT/MPS ]

  1. /*
  2. Here's something that some of you might find useful.  It's a short
  3. C program for MPW 3.0 that's just barely smart enough to unpack shar files
  4. from comp.sources.unix.  It does create subdirectories
  5. if necessary, and it's orders of magnitude faster than the MPW shell
  6. unshar that was posted a while back.  The source is a lot shorter than
  7. the binary, so I thought I'd just post it--that way folks who use THINK C
  8. and so on can tweak it themselves if they want...
  9. */
  10.  
  11. /* unshar for MPW that's good enough for comp.sources.unix archives */
  12. #include <stdio.h>
  13. #include <Files.h>
  14. #include <Errors.h>
  15.  
  16. int force = 0;  /* force overwriting existing files */
  17. char *progname;
  18.  
  19. main(argc, argv)
  20. int argc;
  21. char **argv;
  22. {
  23.   progname = *argv;
  24.  
  25.   if (argc == 1) {
  26.     fprintf(stderr, "### %s - Not enough parameters were specified.\n",        progname);
  27.     fprintf(stderr, "# Usage - %s [-f] file [file...]\n", progname);
  28.   } else while (--argc) {
  29.     if (**++argv == '-') {
  30.       switch ((*argv)[1]) {
  31.         case 'f':
  32.           force = 1;
  33.         break;      }
  34.     } else
  35.       unshar(*argv);
  36.   }
  37. }
  38.  
  39. unshar(s)
  40. char *s;
  41. {
  42.   char buffer[BUFSIZ];
  43.   char *cp;
  44.   int force;
  45.   FILE *infp, *outfp;
  46.   char unixfilename[64], mpwfilename[64];
  47.   int line;
  48.   short v;
  49.   long dirID, dID;
  50.  
  51.   infp = fopen(s, "r");
  52.   if (!infp) {    fprintf(stderr, "### %s - Could not open file %s\n", progname, s);
  53.     return;
  54.   }
  55.  
  56.   /* skip over news header lines etc. */
  57.   for (line = 1; cp = fgets(buffer, sizeof(buffer), infp); line++)
  58.     if (buffer[0] == '#') break;
  59.   if (!cp) {
  60.     fprintf(stderr, "### %s - Could not locate start of archive in file %s\n",            progname, s);
  61.     return;
  62.   }
  63.  
  64.   /* now we should be at the start of the shar archive itself */
  65.   while (cp = fgets(buffer, sizeof(buffer), infp)) {
  66.     line++;
  67.     if (buffer[0] == '#') continue;    /* comment line */    if (buffer[0] == 'e') { fclose(infp); return; }  /* exit */
  68.     if (buffer[0] == 'i') {
  69.         if (!strncmp(buffer, "if test -f", 10)) {
  70.         /* testing to see if a file is there */
  71.         if (sscanf(buffer, "if test -f '%s'", unixfilename) == 1) {          /* make Mac relative pathname */
  72.           sprintf(mpwfilename, ":%s", unixfilename);
  73.           /* convert '/' to ':' & vice versa */
  74.           for (cp = mpwfilename+1; *cp; cp++)
  75.             if (*cp == '/')              *cp = ':';
  76.             else if (*cp == ':')
  77.               *cp = '/';
  78.           cp[-1] = 0;  /* drop trailing quote mark */
  79.           outfp = fopen(mpwfilename, "r");
  80.           if (outfp && !force) {
  81.             fclose(outfp);
  82.             fprintf(stderr, "### %s - Will not clobber existing file '%s'\n",                progname, mpwfilename);
  83.             while (buffer[0] != 'f') {  /* skip to size "fi" */
  84.               fgets(buffer, sizeof(buffer), infp);
  85.             }
  86.             fgets(buffer, sizeof(buffer), infp);  /* skip over ending comment */            fgets(buffer, sizeof(buffer), infp);  /* skip over test "fi" */
  87.           } else {
  88.             if (outfp) fclose(outfp);
  89.             create(mpwfilename, 0, 'MPS ', 'TEXT');
  90.             outfp = fopen(mpwfilename, "w");            fprintf(stderr, "### %s - Extracting '%s'\n", mpwfilename);
  91.             while (buffer[0] != 'X')
  92.               fgets(buffer, sizeof(buffer), infp);
  93.             do {
  94.               fputs(buffer+1, outfp);              fgets(buffer, sizeof(buffer), infp);
  95.             } while (buffer[0] == 'X');
  96.             fclose(outfp);
  97.             fgets(buffer, sizeof(buffer), infp);  /* skip to next if */
  98.             fgets(buffer, sizeof(buffer), infp);
  99.             fgets(buffer, sizeof(buffer), infp);            fgets(buffer, sizeof(buffer), infp);
  100.             fgets(buffer, sizeof(buffer), infp);
  101.           }
  102.         } else {
  103.           fprintf(stderr, "### %s - Cannot understand 'if' statement, aborting file:\n", progname);          fprintf(stderr, "File %s; Line %d # %s", s, line, buffer);
  104.           fclose(infp);
  105.           return;
  106.         }
  107.       } else if (!strncmp(buffer, "if test ! -d", 12)) {
  108.         /* testing to see if a directory is there */
  109.         if (sscanf(buffer, "if test ! -d '%s'", unixfilename) == 1) {          /* make Mac relative pathname */
  110.           sprintf(mpwfilename, ":%s", unixfilename);
  111.           /* convert '/' to ':' & vice versa */
  112.           for (cp = mpwfilename+1; *cp; cp++)
  113.             if (*cp == '/')              *cp = ':';
  114.             else if (*cp == ':')
  115.               *cp = '/';
  116.           cp[-1] = 0;  /* drop trailing quote mark */
  117.  
  118.           /* I wish MPW C had mkdir(), but at least we don't have to do parameter blocks */
  119.           HGetVol(unixfilename, &v, &dirID);          c2pstr(mpwfilename);
  120.           DirCreate(v, dirID, mpwfilename, &dID);
  121.           fgets(buffer, sizeof(buffer), infp);
  122.           fgets(buffer, sizeof(buffer), infp);
  123.           fgets(buffer, sizeof(buffer), infp);
  124.         }      }
  125.     }
  126.   }
  127.   fclose(infp);
  128. }
  129.